<?php
declare (strict_types=1);

namespace app\course\logic\live\admin;

use app\common\exception\LogicException;
use app\course\logic\base\{CourseSection};
use app\course\logic\live\BokeKT;
use app\course\logic\live\BokeZB;
use app\course\model\live\CourseSectionLive as LiveSectionModel;

/**
 * CC接口逻辑层
 * Author 亓官雨树<lucky.max@foxmail.com>
 * Date：   2020-10-27
 */
class LiveBoke extends LiveSectionModel
{

    /**
     * 创建CC云直播间
     */
    public function createZBRoom($param)
    {
        return $this->createCCRoom($param, 'zb');
    }

    /**
     * 创建CC课堂直播间
     */
    public function createKTRoom($param)
    {
        return $this->createCCRoom($param, 'kt');
    }

    /**
     * CC直播间
     */
    public function createCCRoom($param, $type)
    {
        // 参数
        $sid = $param['section_id'];
        unset($param['section_id']);
        // $mid = request()->mid;
        // 大班课
        $zb = ($type == 'zb');
        // 直播间类型错误验证
        if (!in_array($type, ['zb', 'kt'])) throw new LogicException('参数错误');
        // 检查开播用户身份：讲师身份
        // $teacher = app(Teacher::class)->where('user_id', $mid)->findOrEmpty();
        // if ($teacher->isEmpty()) throw new LogicException('当前身份无开播权限');
        // 检查房间号是否正确
        $section = app(CourseSection::class)->with(['live_type', 'limit_buy'])->findOrEmpty($sid);
        //
        // if (!$section->is_last || $section->course_type != 2 || $section->live_provider != 3) throw new LogicException('未找到课时');
        // if ($section->live_type == 1 && !$zb) throw new LogicException('课程信息错误');
        // if ($section->live_type == 2 && $zb) throw new LogicException('课程信息错误');
        // 课时名称
        $param['name'] = $param['desc'] = $section->title;
        // 小班课人数
        if (!$zb) {
            // 课程人数限制
            $limit = $section->limit_buy;
            // $param['max_streams'] = $limit == 1 ? 2 : ( floor($limit / 3) + ($limit % 3 ? 1 : 0) ) * 3 + 1;
            $param['max_streams'] = $limit == 1 ? 2 : floor($limit / 3) * 3 + ($limit % 3 ? 4 : 1);
        }
        // 非当前课程讲师
        // $course = app(Course::class)->findOrEmpty($section->course_id);
        // if ($course->teacher_id != $teacher->id) throw new LogicException('当前身份无开播权限');
        // 存在就返回，不存在就创建
        $data = $this->where('room_no', $sid)->findOrEmpty();
        if ($data->isEmpty()) {
            $roomid = cache('cc_room_' . $sid);
            $boke   = $zb ? new BokeZB(intval($section->course_id)) : new BokeKT(intval($section->course_id));
            if (!$roomid) {
                $roomid = $boke->createRoom($param);
                cache('cc_room_' . $sid, $roomid);
            }
            $save['room_no']       = $sid;
            $save['live_type']     = $zb ? 2 : 3;
            $save['authtype']      = 0;
            $save['cc_room_id']    = $roomid;
            $save['cc_userid']     = $boke->get_user_id();
            $save['cc_live_ids']   = [];
            $save['cc_record_ids'] = [];
            $save['course_id']     = $section->course_id;
            $save['close_time']    = $section->end_time;

            if ($this::create($save)) {
                cache('cc_room_' . $sid, null);
                return $roomid;
            } else {
                throw new LogicException('回调失败');
            }
        }
        return $data->cc_room_id;
    }

}
